;
! function() {
	var layer = layui.layer,
		form = layui.form,
		laypage = layui.laypage;

	var status = null;
	
	layer.load(2);

	to_page(1, 10);

	// 请求分页数据
	function to_page(pageNum, pageSize) {
		$.ajax({
			url: "/secuser/",
			type: "post",
			data: {
				"pageNum": pageNum,
				"pageSize": pageSize,
				"status": status
			},
			async: false,
			success: function(result) {
				layer.closeAll('loading');
				if(result.totalRow == 0) {
					// 提示没有数据
					$("#content").html("<tr><td colspan='4' style='text-align:center;font-size: 18px;color:#FF6700;'>没有查询到任何数据哦~</td></tr>");
					// 隐藏分页条
					$("#page_info").hide();
				} else {
					// 拼装tr并赋给页面
					build_table(result);
					// 拼装分页总览信息并赋给页面
					build_pageInfo(result);
					// 显示分页按钮
					laypage.render({
						elem: 'page_nav_div',
						count: result.totalRow,
						curr: result.pageNumber,
						limit: result.pageSize,
						theme: "#FF6700",
						prev: "<em>«</em>",
						next: "<em>»</em>",
						layout: ['prev', 'page', 'next', 'limit', 'skip'],
						jump: function(obj, first) {
							if(!first) {
								to_page(obj.curr, obj.limit);
							}
						}
					});
					// 显示分页条
					$("#page_info").show();
				}
			}
		});
	}

	// 构建分页细节
	function build_pageInfo(pageInfo) {
		$("#pagination_overview").html(
			"当前第" + pageInfo.pageNumber + "页," +
			"共有" + pageInfo.totalPage +
			"页,总计" +
			pageInfo.totalRow + "条记录");
	}

	// 构建表格
	function build_table(page) {
		// 清空tbody
		$("#content").empty();
		// 遍历数据并累加
		$.each(page.list, function() {
			var headUrlTd = $("<td class='title-td'></td>").append("<img style='width: 45px;height: 45px;border-radius: 2px;' src='" + (this.headImgUrl == "" ? "/static/img/user.png" : this.headImgUrl) + "'>");
			var userNameTd = $("<td></td>").append(this.userName);
			var statusTd = $("<td></td>").append(this.status == '该账户已被冻结' ? '<span style="color:red;">' + this.status + '</span>' : this.status);
			var phoneTd = $("<td></td>").append(this.phone);
			var operation = $("<td>" +
				"<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-normal audit-info'>" +
				"<i class='layui-icon'>&#xe615;</i>查看认证信息</button>" +
				"<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-warm secuser-info'>" +
				"<i class='layui-icon'>&#xe615;</i>查看用户信息</button>" +
				"<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-danger status'>" +
				"<i class='layui-icon'>&#xe614;</i>" + (this.status == '该账户已被冻结' ? '解冻用户' : '冻结用户') + "</button>" +
				"</td>");
			// 赋给tbody
			$("<tr id='" + this.id + "'></tr>").append(headUrlTd).append(userNameTd).append(statusTd).append(phoneTd).append(operation).appendTo("#content");
		});
	}

	//查看认证信息
	$(document).on("click", ".audit-info", function() {
		var id = $(this).parent().parent().attr("id");
		$.ajax({
			url: "/secuser/judgeWhetherRealName",
			type: "post",
			data: {
				"id": id
			},
			success: function(result) {
				if(result.status == 0) {
					location = "/auditingRealName/" + id;
				} else {
					layer.msg(result.msg);
				}
			}
		});
	});
	
	//查看认证信息
	form.on('select(secUserState)', function(data) {
		status = data.value;
		to_page(1, 10);
	});

	//冻结用户
	$(document).on("click", ".status", function() {
		var id = $(this).parent().parent().attr("id");
		var type = $(this).html();
		type = type.substring(type.lastIndexOf(">") + 1, type.length);
		layer.confirm("确定要" + type + "吗?", {
			icon: 3,
			title: '温馨提示'
		}, function(index) {
			$.ajax({
				url: "/secuser/updateStatus",
				type: "post",
				data: {
					"id": id,
					"type": type
				},
				success: function(result) {
					layer.close(index);
					if(result.status == 0) {
						layer.msg(type + "成功");
						to_page(1, 10);
					} else {
						layer.msg(result.msg);
					}
				}
			});
		});
	});

	$(document).on("click", ".secuser-info", function() {
		var id = $(this).parent().parent().attr("id");
		if(id != "" && id != null) {
			location.href = "/secUserInfo/" + id;
		}
	});
}();